import { POST } from "@net/service.js";
import $ from "@util/base";
import page from "./page"

const URL = {
    list: "coupon_list",
    add: "coupon_add",
    details: "coupon_detail",
    provide: "coupon_provide",
    export: "coupon_list_export"
};

const api = {
    list(params, succ) {
        let paramsData = {};
        if ($.isNotEmpty(params.name)) {
            paramsData.name = params.name;
        }
        paramsData.page = params.page ? params.page : 1;
        paramsData.pageTotal = params.size ? parseInt(params.size) : page.size;
        let requestOptions = {
            path: URL.list,
            params: paramsData,
            isLoading: true,
            succ: succ,
        };
        POST(requestOptions);
    },
    add(params, succ) {
        let paramsData = {}
        paramsData.name = params.name
        paramsData.startDate = params.date[0]
        paramsData.endDate = params.date[1]
        paramsData.moneyType = params.moneyType
        paramsData.money = params.moneyType === "1010001" ? params.moneyOff.money : params.discountMoney
        paramsData.limitMoney = params.moneyType === "1010001" ? params.moneyOff.limitMoney : ""
        paramsData.limitBusiType = params.limitBusiType
        let requestOptions = {
            path: URL.add,
            params: paramsData,
            isLoading: true,
            succ: succ,
        };
        POST(requestOptions);
    },
    details(code, succ) {
        let requestOptions = {
            path: URL.details,
            params: { couponCode: code },
            isLoading: true,
            succ: val => {
                let couponData = {
                    moneyOff: {}
                }
                couponData.date = [val.startDate, val.endDate]
                if (val.moneyType === '1010001') {
                    couponData.moneyOff.limitMoney = val.limitMoney
                    couponData.moneyOff.money = val.money
                } else {
                    couponData.discountMoney = val.money
                }
                let data = Object.assign(val, couponData)
                succ(data)
            },
        };
        POST(requestOptions);
    },
    provide(params, succ) {
        let paramsData = {}
        paramsData.couponCode = params.couponCode
        paramsData.provideFlag = parseInt(params.provideFlag)
        if (paramsData.provideFlag == 2) {
            paramsData.memberLevel = "1039001"
        }
        let requestOptions = {
            path: URL.provide,
            params: paramsData,
            isLoading: true,
            succ: succ,
        };
        POST(requestOptions);
    },
    export (params, succ) {
        let paramsData = {};
        if ($.isNotEmpty(params.name)) {
            paramsData.name = params.name;
        }
        let requestOptions = {
            path: URL.export,
            params: paramsData,
            succ: succ,
        };
        POST(requestOptions);
    },
}

export default api;